package com.ruoyi.aiexperiments.controller;

import java.util.Date;
import java.util.List;



import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.aiexperiments.domain.AlgorithmType;
import com.ruoyi.aiexperiments.service.IAlgorithmTypeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 算法类型Controller
 * 
 * @author czj
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/type")
public class AlgorithmTypeController extends BaseController
{
    @Autowired
    private IAlgorithmTypeService algorithmTypeService;

    /**
     * 查询算法类型列表
     */
    @RequiresPermissions("aiexperiments:type:list")
    @GetMapping("/list")
    public TableDataInfo list(AlgorithmType algorithmType)
    {
        algorithmType.setDeleteFlag(0);
        startPage();
        List<AlgorithmType> list = algorithmTypeService.selectAlgorithmTypeList(algorithmType);
        return getDataTable(list);
    }

    //@RequiresPermissions("aiexperiments:type:list")
    @PostMapping("/list1")
    public List<AlgorithmType> list1(AlgorithmType algorithmType)
    {
        algorithmType.setDeleteFlag(0);
        startPage();
        List<AlgorithmType> list = algorithmTypeService.selectAlgorithmTypeList(algorithmType);
        return list;
    }

    /**
     * 导出算法类型列表
     */
    @RequiresPermissions("aiexperiments:type:export")
    @Log(title = "算法类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgorithmType algorithmType)
    {
        List<AlgorithmType> list = algorithmTypeService.selectAlgorithmTypeList(algorithmType);
        ExcelUtil<AlgorithmType> util = new ExcelUtil<AlgorithmType>(AlgorithmType.class);
        util.exportExcel(response, list, "算法类型数据");
    }

    /**
     * 获取算法类型详细信息
     */
    @RequiresPermissions("aiexperiments:type:query")
    @GetMapping(value = "/{algorithmId}")
    public AjaxResult getInfo(@PathVariable("algorithmId") Long algorithmId)
    {
        return success(algorithmTypeService.selectAlgorithmTypeByAlgorithmId(algorithmId));
    }

    /**
     * 新增算法类型
     */
    @RequiresPermissions("aiexperiments:type:add")
    @Log(title = "算法类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AlgorithmType algorithmType)
    {
        algorithmType.setCreatedBy(SecurityUtils.getUsername());
        algorithmType.setCreatedAt(new Date());
        return toAjax(algorithmTypeService.insertAlgorithmType(algorithmType));
    }

    /**
     * 修改算法类型
     */
    @RequiresPermissions("aiexperiments:type:edit")
    @Log(title = "算法类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AlgorithmType algorithmType)
    {
        if (algorithmType.getReviewStatus().equals("1")){
            algorithmType.setReviewStatus("0");
        }else {
            algorithmType.setReviewerId(SecurityUtils.getUsername());
            algorithmType.setReviewDate(new Date());
        }
        algorithmType.setUpdatedBy(SecurityUtils.getUsername());
        algorithmType.setUpdatedAt(new Date());
        return toAjax(algorithmTypeService.updateAlgorithmType(algorithmType));
    }

    /**
     * 删除算法类型
     */
    @RequiresPermissions("aiexperiments:type:remove")
    @Log(title = "算法类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{algorithmIds}")
    public AjaxResult remove(@PathVariable Long[] algorithmIds)
    {
        for (Long algorithmId : algorithmIds) {
            AlgorithmType algorithmType = algorithmTypeService.selectAlgorithmTypeByAlgorithmId(algorithmId);
            if (algorithmType == null) {
                return error("算法类型不存在");
            }

            algorithmType.setDeletedBy(SecurityUtils.getUsername());
            algorithmType.setDeletedAt(new Date());
            algorithmType.setDeleteFlag(1);
            algorithmTypeService.updateAlgorithmType(algorithmType);
        }
        return toAjax(true);
    }
}
